package com.example.cinema.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.example.cinema.entity.*;
import com.example.cinema.entity.service.impl.MovieReviewServiceImpl;
import com.example.cinema.mapper.MovieMapper;
import com.example.cinema.mapper.MovieReviewMapper;
import com.example.cinema.mapper.UserMapper;
import com.yc.sign.web.bean.SignProperties;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import javax.xml.crypto.Data;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yc138
 * @since 2024-08-26
 */
@RestController
@RequestMapping("/movieReview")
public class MovieReviewController {
    @Autowired
    MovieMapper movieMapper;
    @Autowired
    MovieReviewMapper movieReviewMapper;
    @Autowired
    SignProperties properties;
    @Autowired
    MovieReviewServiceImpl movieReviewService;
    @Autowired
    UserMapper userMapper;
    //查找电影的评论
    @RequestMapping("queryReview")
    Page queryReview(@RequestParam int id,
                                  @RequestParam(defaultValue = "1") int current ,
                                  @RequestParam(defaultValue = "5") int size){
        Page<MovieReview> p = new PageDTO<>(current, size);
        LambdaQueryWrapper<MovieReview> lqw=new LambdaQueryWrapper<>();
        lqw.eq(MovieReview::getMid,id);
        lqw.orderByDesc(MovieReview::getReviewDate);
        Page<MovieReview> page1 = movieReviewMapper.selectPage(p, lqw);
        for (int i = 0; i < page1.getRecords().size(); i++) {
            page1.getRecords().get(i).setUser(userMapper.selectById(page1.getRecords().get(i).getUid()));
        }
        return page1;
    }
    //新增评论
    @RequestMapping("addRevies")
    Result addRevies(@RequestParam int id, @RequestParam String content, HttpSession session){
        MovieReview review=new MovieReview();
        User user = (User) session.getAttribute(properties.getSessionKey());
        if(user==null){
            return new Result(0,"请先登录，在评论",null);
        }
        review.setUid(user.getId());
        review.setReviewContent(content);
        review.setMid(id);
        review.setReviewDate(LocalDateTime.now());
        return  movieReviewService.addMovieReview(review);
    }
}

